﻿using System.Linq;
using Vk.Methods.Common;
using Vk.Methods.Places;

namespace Vk.Methods.Groups
{
    /// <summary>
    ///     Параметры группы
    /// </summary>
    public class GroupSettings
    {
        /// <summary>
        ///     Короткий адрес.
        /// </summary>
        public string Address { get; private set; }

        /// <summary>
        ///     Возрастные ограничения
        /// </summary>
        public AgeLimit AgeLimits { get; private set; }

        /// <summary>
        ///     Настройки аудиозаписей.
        /// </summary>
        public int Audio { get; private set; }

        /// <summary>
        ///     Описание сообщества.
        /// </summary>
        public string Description { get; private set; }

        /// <summary>
        ///     Настройки документов.
        /// </summary>
        public int Docs { get; private set; }

        /// <summary>
        ///     Настройки блока товаров.
        /// </summary>
        public MarketSettings Market { get; private set; }

        /// <summary>
        ///     Настройки фотографий.
        /// </summary>
        public int Photos { get; private set; }

        /// <summary>
        ///     Объект, описывающий место.
        /// </summary>
        public Place Place { get; private set; }

        /// <summary>
        ///     Адрес RSS-ленты.
        /// </summary>
        public string Rss { get; private set; }

        /// <summary>
        ///     Тематика.
        /// </summary>
        public GroupSubject Subject { get; private set; }

        /// <summary>
        ///     Название сообщества.
        /// </summary>
        public string Title { get; private set; }

        /// <summary>
        ///     Настройки обсуждений.
        /// </summary>
        public int Topics { get; private set; }

        /// <summary>
        ///     Тип сообщества.
        /// </summary>
        public GroupAccess Type { get; private set; }

        /// <summary>
        ///     Настройки видеозаписей.
        /// </summary>
        public int Video { get; private set; }

        /// <summary>
        ///     Настройки стены.
        /// </summary>
        public int Wall { get; private set; }

        /// <summary>
        ///     Адрес веб-сайта.
        /// </summary>
        public string Website { get; private set; }

        /// <summary>
        ///     Настройки вики-страниц.
        /// </summary>
        public int Wiki { get; private set; }

        public GroupSettings(ApiNode result)
        {
            Title = result.String("title");
            Description = result.String("description");
            Address = result.String("address");
            Place = new Place(result.SelectNode("place"));

            Wall = result.Int("wall").GetValueOrDefault();
            Photos = result.Int("photos").GetValueOrDefault();
            Video = result.Int("video").GetValueOrDefault();
            Audio = result.Int("audio").GetValueOrDefault();
            Docs = result.Int("docs").GetValueOrDefault();
            Topics = result.Int("topics").GetValueOrDefault();
            Wiki = result.Int("wiki").GetValueOrDefault();

            Type = (GroupAccess) result.Int("type");
            Subject = (GroupSubject) result.Int("subject");

            Rss = result.String("rss");
            Website = result.String("website");
            AgeLimits = (AgeLimit) result.Int("age_limits");

            Market = new MarketSettings(result.SelectNode("market"));
        }
    }

    public class MarketSettings
    {
        /// <summary>
        ///     Идентификаторы городов;
        /// </summary>
        public int[] CityIds { get; private set; }

        /// <summary>
        ///     Включены ли комментарии к товарам
        /// </summary>
        public bool CommentsEnabled { get; private set; }

        /// <summary>
        ///     Идентификатор контактного лица;
        /// </summary>
        public int ContactId { get; private set; }

        /// <summary>
        ///     Идентификаторы стран
        /// </summary>
        public int[] CountryIds { get; private set; }

        /// <summary>
        ///     Объект, описывающий валюту.
        /// </summary>
        public IdTitleObject Currency { get; private set; }

        /// <summary>
        ///     Включен ли блок товаров
        /// </summary>
        public bool IsEnabled { get; private set; }

        public MarketSettings(ApiNode apiNode)
        {
            IsEnabled = apiNode.Bool("enabled").Value;
            CommentsEnabled = apiNode.Bool("comments_enabled").Value;
            CountryIds = apiNode.SelectNodes<int>("country_ids/*").ToArray();
            CityIds = apiNode.SelectNodes<int>("city_ids/*").ToArray();
            ContactId = apiNode.Int("contact_id").Value;
            Currency = new IdTitleObject(apiNode.SelectNode("currency"), titleNodeName: "name");
        }
    }
}